#import "CTUploadManager.h"
#import "CTLoginModel.h"
#import "CTLocationModel.h"
#import "CTDeviceModel.h"
#import "CTRegisterModel.h"
#import "CTPermitModel.h"
#import "CTLaunchModel.h"
#import "CTInstallModel.h"
#import "CTPageModel.h"
#import "CTTFModel.h"
#import "CTBtnModel.h"
#import "CTMotionModel.h"
@implementation CTUploadManager
static CTUploadManager *var_instance;
+ (instancetype)ct_shareInstance {
    if (var_instance==nil) {
        var_instance = [[super allocWithZone:NULL] init];
    }
    return var_instance;
}
+ (id)allocWithZone:(struct _NSZone *)zone {
    return [CTUploadManager ct_shareInstance];
}
- (id)copyWithZone:(struct _NSZone *)zone {
    return [CTUploadManager ct_shareInstance];
}
+ (void)ct_destroyInstance {
    var_instance = nil;
}
+ (void)ct_uploadPermissionWithType:(CTPermissionType)type permit:(BOOL)permit {
    NSMutableDictionary *var_param = [CTPermitModel ct_getParameterWithType:type permit:permit];
    [self ct_requestWithParam:var_param type:@"Permit"];
}
+ (void)ct_uploadType:(CTUploadType)type {
    [self ct_uploadType:type data:@{}];
}
+ (void)ct_uploadType:(CTUploadType)type data:(NSDictionary *)data {
    switch (type) {
        case CTUploadTypeLocation: {
            if ([CTLocationManager sharedManager].status == kCLAuthorizationStatusAuthorizedAlways || [CTLocationManager sharedManager].status == kCLAuthorizationStatusAuthorizedWhenInUse) {
                NSMutableDictionary *var_param = [CTLocationModel ct_getParameter];
                [self ct_requestWithParam:var_param type:@"Location"];
            }
        }
            break;
        case CTUploadTypeLogin: {
            NSMutableDictionary *var_param = [CTLoginModel ct_getParameter];
            [self ct_requestWithParam:var_param type:@"Login"];
        }
            break;
        case CTUploadTypeRegister: {
            NSMutableDictionary *var_param = [CTRegisterModel ct_getParameter];
            [self ct_requestWithParam:var_param type:@"Reg02"];
        }
            break;
        case CTUploadTypeDevice: {
            NSMutableDictionary *var_param = [CTDeviceModel ct_getParameter];
            [self ct_requestWithParam:var_param type:@"Device"];
        }
            break;
        case CTUploadTypeLaunch: {
            NSMutableDictionary *var_param = [CTLaunchModel ct_getParameter];
            [self ct_requestWithParam:var_param type:@"Launch"];
        }
            break;
        case CTUploadTypeMotion: {
            NSMutableDictionary *var_param = [CTMotionModel ct_getParameter];
            [var_param addEntriesFromDictionary:data];
            [self ct_requestWithParam:var_param type:@"Motion"];
        }
            break;
        case CTUploadTypeInstalls: {
            NSMutableDictionary *var_param = [CTInstallModel ct_getParameter];
            [self ct_requestWithParam:var_param type:@"Installs"];
        }
            break;
        case CTUploadTypePage: {
            NSMutableDictionary *var_param = [CTPageModel ct_getParameter];
            [var_param addEntriesFromDictionary:data];
            [self ct_requestWithParam:var_param type:@"PAGE"];
        }
            break;
        case CTUploadTypeTF: {
            [self ct_requestWithParam:data.mutableCopy type:@"TF"];
        }
            break;
        case CTUploadTypeBTN: {
            NSMutableDictionary *var_param = [CTBtnModel ct_getParameter];
            [var_param addEntriesFromDictionary:data];
            [self ct_requestWithParam:var_param type:@"BTN"];
        }
            break;
        case CTUploadTypeUpdateRegSubChannel: {
            [self ct_requestUpdateRegSubChannelWithType:@"UpdateRegSubChannel"];
        }
            break;
        case CTUploadTypeCredit: {
            [self ct_requestCreditUploadWitharam:data.mutableCopy];
        }
            break;
        default:
            break;
    }
}
+ (void)ct_requestWithParam:(NSMutableDictionary *)param type:(NSString *)type {
    [CTService reportWithParam:param block:^(CTBaseRequest * _Nonnull operation, BOOL isSuccess, id  _Nonnull responseObject) {
        NSLog(@"------------report type：%@\r\n", type);
        if (isSuccess) {
        } else {
            if (operation.requestStatus == RequestStatusRemoteFaild) {
                [self ct_requestWithParam:param type:type];
            }
        }
    }];
}
+ (void)ct_requestUpdateRegSubChannelWithType:(NSString *)type {
    NSDictionary *dictParam = @{@"userGroup":[CTUserManager shareManager].userModel.userGroup,
                                @"reqSubChannel":[CTAFManager getChannel]
    };
    NSString *url = CTPath(@"/accountProfileUpdate", CTPathTypePost, nil);
    [CTBaseRequest postRequestWithMethodName:url parametersDic:dictParam success:^(CTBaseRequest * _Nonnull operation, BOOL isSuccess, id  _Nonnull responseObject) {
        if (isSuccess) {
            NSLog(@"------------UpdateRegSubChannel success：%@", type);
        } else {
            NSLog(@"------------UpdateRegSubChannel failure：%@", type);
            if (operation.requestStatus == RequestStatusRemoteFaild) {
                [self ct_requestUpdateRegSubChannelWithType:type];
            }
        }
    }];
}
+ (void)ct_requestCreditUploadWitharam:(NSMutableDictionary *)param {
    NSString *url = CTPath(@"/grantingPoOpt", CTPathTypeCredit, nil);
    [CTBaseRequest postRequestWithMethodName:url parametersDic:param success:^(CTBaseRequest * _Nonnull operation, BOOL isSuccess, id  _Nonnull responseObject) {
        if (isSuccess) {
            NSLog(@"------------report success Credit");
        } else {
            NSLog(@"------------report failure Credit");
            if (operation.requestStatus == RequestStatusRemoteFaild) {
                [self ct_requestCreditUploadWitharam:param];
            }
        }
    }];
}
@end
